﻿using System;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using log4net.Config;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Validator.Cfg.Loquacious;
using NHibernate.Validator.Engine;

namespace hbse
{
    static class Program
    {
       
        [STAThread]
        static void Main()
        {
            AppConfigure();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmMain());
        }
        private static void AppConfigure()
        {
 
            XmlConfigurator.Configure();
           
            var conf = new Configuration();
            try
            {
                conf.Configure();
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
              
           
           
            try
            {
                new SchemaValidator(conf).Validate();
            }
            catch (HibernateException)
            {
                var export = new SchemaExport(conf);
                export.Drop(false, true);
                export.Create(false, true);
            }

            var validatorConf = new FluentConfiguration();
            validatorConf.SetDefaultValidatorMode(ValidatorMode.UseExternal).Register(
                Assembly.GetExecutingAssembly().GetTypes().Where(t => t.Namespace.Equals("hbse.Mappings")).
                    ValidationDefinitions());
            var validatorEngine = new ValidatorEngine();
            validatorEngine.Configure(validatorConf);

            ApplicationContext.SessionFactory = conf.BuildSessionFactory();
            ApplicationContext.Validator = validatorEngine;
        }
    }
}
